import React from 'react';
import {Input, Modal} from 'antd';
import {ModalFuncProps, ModalProps} from "antd/lib/modal/Modal";
import ReactDOM from "react-dom";
import "./HYModal.less"
import ClassNames from "classnames";

export interface HYModalProps extends ModalProps {
}

class HYModal extends React.Component<HYModalProps, any> {
  modal: Modal;
  buttons: [];

  constructor(props) {
    super(props);
  }

  componentDidMount() {
  }

  componentDidUpdate() {
    console.log("HYModal.componentDidUpdate", this.modal);
    if (!this.buttons || this.buttons.length === 0) {
      this.addClass();
    }
  }

  /**
   * 给按钮添加hy-btn class
   */
  private addClass() {
    let node = ReactDOM.findDOMNode(this.modal);
    if (!node) {
      setTimeout(() => this.addClass(), 1);
    } else {
      // @ts-ignore
      let elements = node.querySelectorAll('button.ant-btn');
      this.buttons = elements;
      for (let i = 0; i < elements.length; i++) {
        elements[i].classList.add("hy-btn");
      }
    }
  }

  render() {
    const {className, ...rest} = this.props;
    return <Modal ref={(e) => {
      this.modal = e
    }} className={ClassNames("hy-modal",className)} {...rest} >
      <span>{this.props.children}</span>
    </Modal>;
  }

  public static confirm =(props: ModalFuncProps)=>{
    Modal.confirm(props)
  };
  public static info =(props: ModalFuncProps)=>{
    props.okText = props.okText ? props.okText : "我知道了";
    Modal.info(props)
  };
}

export default HYModal;
